<?php 
	$PATH = $_SERVER['PHP_SELF']; 
	$filename = "users_to_add.csv";
	$from = "NEWBIES INFO <info@newbiesvn.com>";
	$subject = "CSE Forum's Password";
	$message = 
		"Hello {{id}},<br />\n".
		"<br />\n".
		"Here is your password for CSE Forum: <b>{{pwd}}</b><br />\n".
		"Do not reply this email.";
?>
<html>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<head>
	<title>Send Password</title>
	<style type="text/css">
		label {
			width: 200px;
			display: inline-block;
		}
		input[type=text] {
			width: 300px;
			display: inline-block;
		}
		div {
			display: inline-block;
			width: 510px;
			height: 310px;
			vertical-align: top;
		}
		#preview {border: 1px dashed grey}
		textarea{
			width: 500px;
			height: 300px;
		}

	</style>
	<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
</head>
<body>
	<h2>Hướng dẫn:</h2>
	<ul>
		<li>{{id}} và {{pwd}} sẽ được thay thế lần lượt bằng MSSV và password của từng sinh viên (đọc từ file danh sách)</li>
		<li>Phần Message là nội dung email, viết bằng HTML</li>
	</ul>

	<hr>

	<form method="POST" action="<?php echo $PATH?>">
		<label>Tên file danh sách (.csv):</label><input type="text" name="filename" value="<?php echo $filename?>"><br />
		<label>Địa chỉ gửi email:</label><input type="text" name="from" value="<?php echo $from?>"><br />
		<label>Tiêu đề email:</label><input type="text" name="subject" value="<?php echo $subject?>"><br />
		<label>Nội dung email:</label><br />
		<div id="message-wrapper">
			<textarea id="message" name="message" ><?php echo $message?></textarea><br />
		</div>
		<div id="preview"></div>
		<br />
		<input type="submit" name="submit" value="Submit">
	</form>
	<script type="text/javascript">
		$(document).ready(function() {
			$("#preview").html($("#message").val());
			$("#message").keydown(function(){
				$("#preview").html($(this).val());
			})
		});

	</script>
<?php 
	$USERS_LIST_FILENAME = $_POST['filename'];
	$GROUP_ID = $_POST['group_id'];


	if ($_POST['submit'] != null) {
		
		$filename = $_POST['filename'];
		$from = $_POST['from'];
		$subject = $_POST['subject'];
		$message = $_POST['message'];
		
		//var_dump($_POST);

		$users_counter = 0;
		$handle = fopen($filename, 'r');
		while (($data = fgetcsv($handle, 0, ',')) !== false) {
			//var_dump($data);
			$id = $data[0];
			$pwd = $data[1];
			$email = $data[2];

			$id = intval($id);
			if ($id > 0) {
				$sj = stripslashes($subject);
				$sj = str_replace("{{id}}", $id, $sj);
				$sj = str_replace("{{pwd}}", $pwd, $sj);
				$ms = stripslashes($message);
				$ms = str_replace("{{id}}", $id, $ms);
				$ms = str_replace("{{pwd}}", $pwd, $ms);
				mail_utf8($from, $email, $sj, $ms);
			}		   
		}
		fclose($handle);
		echo "Create file completed! <br />\n";
		echo "<a href='$USERS_LIST_FILENAME'>Download CSV</a>";
	}

?>
</body>
</html>

<?php
	function randStr($length = 10) {
	    return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-_=+"), 0, $length);
	}

	function mail_utf8($from, $to, $subject = '(No subject)', $message = '') 
	{
		$headers = "From: $from\r\n";
		$headers .= "Reply-To: $from\r\n";
		$headers .= "Return-Path: $from\r\n";
		$headers .= "MIME-Version: 1.0\n";
		$headers .= "Content-type: text/html; charset=UTF-8\n";
	  	mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $headers);
	  	//echo "[$from][$to][$subject][$message] <br />";
	}
?>
